package com.chookss.video

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.chookss.R
import com.chookss.Urls
import com.chookss.WebCommonActivity
import com.chookss.base.Token.token
import com.chookss.home.entity.NoticeListEntity
import com.chookss.tools.RecyclerClickAdapter
import com.chookss.tools.Utils
import com.chookss.video.entity.VideoEntity
import com.chookss.video.model.VideoModel
import com.chookss.view.MyImageSpan
import com.johnrambo.ktea.ktExtends.observe
import errorToast
import kotlinx.android.synthetic.main.fragment_video_recommends.*
import kotlinx.android.synthetic.main.item_video_recommends_1.view.*
import kotlinx.android.synthetic.main.layout_common_empty.*

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/26 18:48
 * Note:
 */
class VideoRecommendsFragment1 : Fragment() {
    fun VideoRecommendsFragment1() {}
    private var emptyParams: LinearLayout.LayoutParams? = null
    lateinit var adapter:MyAdapter
    private val videoModel by lazy {
        ViewModelProviders.of(this).get(VideoModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_video_recommends, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = MyAdapter()
        recyclerView.adapter = adapter
        tvEmpty.text="暂无精选数据"
        ivEmpty.setBackgroundResource(R.drawable.icon_default_no_search)
         emptyParams = ivEmpty?.getLayoutParams() as LinearLayout.LayoutParams?
        emptyParams?.setMargins(0, Utils.dip2px(context, 40f), 0, 0)
        ivEmpty?.setLayoutParams(emptyParams)
        recyclerView.layoutManager = GridLayoutManager(view.context, 2)
        videoModel.selectRecommend("1").observe(viewLifecycleOwner, {
            if (it.error != null) {
                activity?.errorToast(it.error.message?:"")
            } else {
                adapter.datas = it.data
                adapter.notifyDataSetChanged()
                if(it.data.size==0){
                    recyclerView.visibility=View.GONE
                    llEmpty.visibility=View.VISIBLE
                }
            }
        })
        adapter.setOnItemClickListener { adapter, data, holder ->
            startActivity(Intent(context, VideoSetsPlayActivity::class.java).apply {
                putExtra("video", data)
                putExtra("type", "2")
                putExtra("watermark", "4")
            })
        }
    }

    class MyAdapter : RecyclerClickAdapter<VideoEntity, RecyclerView.ViewHolder>() {
        var datas: List<VideoEntity>? = null
        override fun getItemCount() = datas?.size ?: 0

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return object:RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_video_recommends_1, parent, false)) {}
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            var view = holder.itemView
            var item = datas!![position]
            view.tvName.text = item.videoCollectionsName
        }

        override fun getItem(position: Int) = datas!![position]
    }

}